﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Krile.Kernel
{
    public static class EnumExtension
    {
        public static bool TryParse<T>(this T theEnum, object value, out T result)
        {
            result = theEnum;
            foreach (string item in Enum.GetNames(typeof(T)))
            {
                if (item.Equals(value))
                {
                    result = (T)Enum.Parse(typeof(T), value.ToString());
                    return true;
                }
            }

            if (Enum.IsDefined(typeof(T), Convert.ChangeType(value, (Enum.GetUnderlyingType(typeof(T))))))
            {
                result = (T)Enum.Parse(typeof(T), value.ToString());
                return true;
            }
            return false;
        }
    }
}
